"""
Virtual Media api
"""


import json

from .command import raw_command
from .argument import build_args
from redfishtoollib import main


# eject virtual media
def eject_first_virtual_media():
    args = build_args()
    cmd = raw_command(args, 'GET', '/redfish/v1/Managers', None)
    res = main(cmd)
    try:
        urls = json.loads(res)
    except ValueError:
        urls = {}
    if len(urls.get('Members', [])) > 0:
        base_url = urls.get('Members', [])[0].get('@odata.id')
        cmd = raw_command(args, 'GET', base_url + '/VirtualMedia', None)
        res = main(cmd)
        try:
            vms = json.loads(res)
        except ValueError:
            vms = {}
        urls = list(filter(lambda x: 'CD' in x['@odata.id'], vms['Members']))
        if len(urls) > 0:
            eject_url = urls[0]['@odata.id'] + '/Actions/VirtualMedia.EjectMedia'
            cmd = raw_command(args, 'POST', eject_url, '{}')
            res = main(cmd)
            try:
                eject_res = json.loads(res)
            except ValueError:
                eject_res = {}
            print(eject_res.get('Status'))


# inject virtual media
# params: nfs url
def inject_first_virtual_media():
    args = build_args()
    cmd = raw_command(args, 'GET', '/redfish/v1/Managers', None)
    res = main(cmd)
    try:
        urls = json.loads(res)
    except ValueError:
        urls = {}
    if len(urls.get('Members', [])) > 0:
        base_url = urls.get('Members', [])[0].get('@odata.id')
        cmd = raw_command(args, 'GET', base_url + '/VirtualMedia', None)
        res = main(cmd)
        try:
            vms = json.loads(res)
        except ValueError:
            vms = {}
        urls = list(filter(lambda x: 'CD' in x['@odata.id'], vms['Members']))
        if len(urls) > 0:
            inject_url = urls[0]['@odata.id'] + '/Actions/VirtualMedia.InsertMedia'
            cmd = raw_command(args, 'POST', inject_url, '{"Image": "%s"}' % args.d)
            res = main(cmd)
            try:
                eject_res = json.loads(res)
            except ValueError:
                eject_res = {}
            print(eject_res.get('Status'))


# eject and inject virtual media
# params: nfs url
def connect_to_first_virtual_media():
    args = build_args()
    cmd = raw_command(args, 'GET', '/redfish/v1/Managers', None)
    res = main(cmd)
    try:
        urls = json.loads(res)
    except ValueError:
        urls = {}
    if len(urls.get('Members', [])) > 0:
        base_url = urls.get('Members', [])[0].get('@odata.id')
        cmd = raw_command(args, 'GET', base_url + '/VirtualMedia', None)
        res = main(cmd)
        try:
            vms = json.loads(res)
        except ValueError:
            vms = {}
        urls = list(filter(lambda x: 'CD' in x['@odata.id'], vms['Members']))
        if len(urls) > 0:
            eject_url = urls[0]['@odata.id'] + '/Actions/VirtualMedia.EjectMedia'
            cmd = raw_command(args, 'POST', eject_url, '{}')
            res = main(cmd)
            try:
                eject_res = json.loads(res)
            except ValueError:
                eject_res = {}
            if eject_res.get('Status') == 'Success':
                inject_url = urls[0]['@odata.id'] + '/Actions/VirtualMedia.InsertMedia'
                cmd = raw_command(args, 'POST', inject_url, '{"Image": "%s"}' % args.d)
                res = main(cmd)
                try:
                    eject_res = json.loads(res)
                except ValueError:
                    eject_res = {}
                print(eject_res.get('Status'))